local MakePlayerCharacter = require 'prefabs/player_common'

local avatar_name = 'doro'
local assets = {
	Asset('SCRIPT', 'scripts/prefabs/player_common.lua'),
	Asset('ANIM', 'anim/' .. avatar_name .. '.zip'),
	Asset('ANIM', 'anim/ghost_' .. avatar_name .. '_build.zip'),
}

local prefabs = {}

local start_inv = {}
-- for k, v in pairs(TUNING.GAMEMODE_STARTING_ITEMS) do
-- 	start_inv[string.lower(k)] = v[string.upper(avatar_name)]
-- end
start_inv['default'] = {}
for k, v in pairs(TUNING.DORO_CUSTOM_START_INV) do
	for i = 1, v.num do
		table.insert(start_inv['default'], k)
	end
end

prefabs = FlattenTree({ prefabs, start_inv }, true)
---------------------------------------------------------------------------

---------------------------------------------------------------------------
local function onbecamehuman(inst, data, isloading)
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, avatar_name .. '_speed_mod', 1)
end

local function onbecameghost(inst, data)
	inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, avatar_name .. '_speed_mod')
end
---------------------------------------------------------------------------
---------------------------------------------------------------------------
local function onload(inst, data)
	inst:ListenForEvent('ms_respawnedfromghost', onbecamehuman)
	inst:ListenForEvent('ms_becameghost', onbecameghost)

	if inst:HasTag('playerghost') then
		onbecameghost(inst)
	else
		onbecamehuman(inst)
	end
end
---------------------------------------------------------------------------
---------------------------------------------------------------------------
local function onhungerdelta(inst, data)
	local hunger         = inst.components.hunger
	local current        = hunger.current
	local locomotor      = inst.components.locomotor
	local combat         = inst.components.combat
	local talker         = inst.components.talker
	local workmultiplier = inst.components.workmultiplier
	if current <= 0 then
		if hunger.doro_level ~= 0 then
			hunger.doro_level = 0
			--[[ if talker then
				talker:Say('doro要吃饭了')
			end ]]
			if locomotor then
				locomotor:SetExternalSpeedMultiplier(inst, 'doro_hunger_speed', 1 / 3)
			end
			if combat then
				combat.externaldamagemultipliers:SetModifier(inst, 5, 'doro_hunger_attack')
				combat.externaldamagetakenmultipliers:SetModifier(inst, 1, 'doro_hunger_defense')
			end
			if hunger then
				hunger.burnratemodifiers:SetModifier(inst, 10, 'doro_hunger_hunger')
			end
			if workmultiplier then
				workmultiplier:SetSpecialMultiplierFn(nil)
			end
		end
	elseif hunger.doro_level == 0 and current < 1000 then
	elseif current < 1000 then
		--移速2.0，攻击倍率5，饥饿速率10，受击倍率1，工作效率1
		if hunger.doro_level ~= 1 then
			hunger.doro_level = 1
			if talker then
				talker:Say('doro要吃饭了')
			end
			if locomotor then
				locomotor:SetExternalSpeedMultiplier(inst, 'doro_hunger_speed', 2)
			end
			if combat then
				combat.externaldamagemultipliers:SetModifier(inst, 5, 'doro_hunger_attack')
				combat.externaldamagetakenmultipliers:SetModifier(inst, 1, 'doro_hunger_defense')
			end
			if hunger then
				hunger.burnratemodifiers:SetModifier(inst, 10, 'doro_hunger_hunger')
			end
			if workmultiplier then
				workmultiplier:SetSpecialMultiplierFn(nil)
			end
		end
	elseif current <= 7500 then
		--移速1.5，攻击倍率2，饥饿速度5，受击倍率1，工作效率2.5
		if hunger.doro_level ~= 2 then
			hunger.doro_level = 2
			inst.components.talker:Say('搞点哦润吉吃吃')
			if locomotor then
				locomotor:SetExternalSpeedMultiplier(inst, 'doro_hunger_speed', 1.5)
			end
			if combat then
				combat.externaldamagemultipliers:SetModifier(inst, 2, 'doro_hunger_attack')
				combat.externaldamagetakenmultipliers:SetModifier(inst, 1, 'doro_hunger_defense')
			end
			if hunger then
				hunger.burnratemodifiers:SetModifier(inst, 5, 'doro_hunger_hunger')
			end
			if workmultiplier then
				workmultiplier:SetSpecialMultiplierFn(function(inst, action, target, tool, numworks, recoil)
					return numworks and numworks * 2.5
				end)
			end
		end
	else
		--移速1.2，攻击倍率1.5，饥饿速度2，受击倍率0.5，工作效率1
		if hunger.doro_level ~= 3 then
			hunger.doro_level = 3
			inst.components.talker:Say('吃饱了就该睡觉了')
			if locomotor then
				locomotor:SetExternalSpeedMultiplier(inst, 'doro_hunger_speed', 1.2)
			end
			if combat then
				combat.externaldamagemultipliers:SetModifier(inst, 1.5, 'doro_hunger_attack')
				combat.externaldamagetakenmultipliers:SetModifier(inst, 0.5, 'doro_hunger_defense')
			end
			if hunger then
				hunger.burnratemodifiers:SetModifier(inst, 2, 'doro_hunger_hunger')
			end
			if workmultiplier then
				workmultiplier:SetSpecialMultiplierFn(nil)
			end
		end
	end
	if hunger.doro_level ~= 0 then
		inst:RemoveTag("groggy")

		inst:RemoveTag("doro_banhands")
	else
		inst:AddTag("groggy")

		inst:AddTag("doro_banhands")
		local hand = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
		if hand then
			inst.components.inventory:GiveItem(hand)
		end
	end
end
local function fakeDoDelta(self, delta, overtime)
	-- 理智值上升下降都比其他角色快20%
	if delta then
		delta = delta * 1.2
	end
	if self.doro_oldDoDelta then
		return self.doro_oldDoDelta(self, delta, overtime)
	end
end
local function onkilled(player, data)
	local victim = data.victim
	local stackmult = data.stackmult or 1
	for i = 1, stackmult do
		if victim.components.lootdropper then
			victim.components.lootdropper:DropLoot(victim:GetPosition())
		end
	end
end
local function doubleIt(inst, loot)
	local pt = inst:GetPosition()
	local stackable = loot.components.stackable
	if stackable and loot:IsValid() then
		stackable:SetStackSize(stackable:StackSize() * 2)
	else
		local new = SpawnPrefab(loot.prefab)
		local inventory = inst.components.inventory
		if inventory and new.components.inventoryitem then
			inventory:GiveItem(new, nil, pt)
		else
			new.Transform:SetPosition(pt:Get())
		end
	end
end
local function onpicksomething(inst, data)
	local loot = data.loot
	local object = data.object
	if object and object.prefab == "moonbase" then return end
	if object and object.prefab == "archive_switch" then return end
	if loot then
		if loot.components then
			doubleIt(inst, loot)
		else
			for i, item in ipairs(loot) do
				doubleIt(inst, item)
			end
		end
	end

	local picker = inst
	if object and object.has_flower then
		-- You get a cactus flower, yay.
		local cactus_flower = SpawnPrefab("cactus_flower")
		cactus_flower.components.inventoryitem:InheritWorldWetnessAtTarget(object)
		if picker.components.inventory ~= nil then
			picker.components.inventory:GiveItem(cactus_flower, nil, object:GetPosition())
		else
			local x, y, z = object.Transform:GetWorldPosition()
			cactus_flower.components.inventoryitem:DoDropPhysics(x, y, z, true)
		end
	end
end
local function custom_stats_mod_fn(inst, health_delta, hunger_delta, sanity_delta, food, feeder)
	if food and food.prefab == "doro_orange_pie" then
		hunger_delta = (hunger_delta or 0) + 15
	end
	return health_delta, hunger_delta, sanity_delta
end
-- 主/客机
local common_postinit = function(inst)
	inst:AddTag(avatar_name)
	--- 可以制作并使用大厨厨具
	inst:AddTag("masterchef")
	inst:AddTag("professionalchef")
	inst:AddTag("expertchef")

	inst.MiniMapEntity:SetIcon(avatar_name .. '.tex')
end
-- 主机
local master_postinit = function(inst)
	inst.starting_inventory = start_inv[TheNet:GetServerGameMode()] or start_inv.default
	inst.soundsname = 'wendy'

	inst.components.health:SetMaxHealth(TUNING[string.upper(avatar_name) .. '_HEALTH'])
	inst.components.hunger:SetMax(TUNING[string.upper(avatar_name) .. '_HUNGER'])
	inst.components.sanity:SetMax(TUNING[string.upper(avatar_name) .. '_SANITY'])
	--自带一本科技
	inst.components.builder.science_bonus = 1

	inst:ListenForEvent('hungerdelta', onhungerdelta)

	inst.components.sanity.doro_oldDoDelta = inst.components.sanity.DoDelta
	inst.components.sanity.DoDelta = fakeDoDelta
	--双倍掉落
	inst:ListenForEvent("killed", onkilled)
	inst:ListenForEvent("picksomething", onpicksomething)

	inst.components.eater.custom_stats_mod_fn = custom_stats_mod_fn

	inst.OnLoad = onload
	inst.OnNewSpawn = onload
end
-- 人物皮肤
local function MakeDOROSkin(name, data, notemp, free)
	local d = {}
	d.rarity = '典藏'
	d.rarityorder = 2
	d.raritycorlor = { 0 / 255, 255 / 255, 249 / 255, 1 }
	d.release_group = -1001
	d.skin_tags = { 'BASE', avatar_name, 'CHARACTER' }
	d.skins = {
		normal_skin = name,
		ghost_skin = 'ghost_' .. avatar_name .. '_build'
	}
	if not free then
		d.checkfn = DORO_API.DOROSkinCheckFn
		d.checkclientfn = DORO_API.DOROSkinCheckFn
	end
	d.share_bigportrait_name = avatar_name
	d.FrameSymbol = 'Reward'
	for k, v in pairs(data) do
		d[k] = v
	end
	DORO_API.MakeCharacterSkin(avatar_name, name, d)
	if not notemp then
		local d2 = shallowcopy(d)
		d2.rarity = '限时体验'
		d2.rarityorder = 80
		d2.raritycorlor = { 0.957, 0.769, 0.188, 1 }
		d2.FrameSymbol = 'heirloom'
		d2.name = data.name .. '(限时)'
		DORO_API.MakeCharacterSkin(avatar_name, name .. '_tmp', d2)
	end
end
function MakeDOROFreeSkin(name, data)
	MakeDOROSkin(name, data, true, true)
end

MakeDOROFreeSkin(avatar_name .. '_none', {
	name = 'Doro', -- 皮肤的名称
	des = 'Doro是一个英文名字，源自希腊语，意为“神的赠礼”，通常用来形容具有神奇魅力和智慧的人[^0^]。', -- 皮肤界面的描述
	quotes = '别想那么多了，一起去吃欧润吉吧。', -- 选人界面的描述
	rarity = '典藏', -- 珍惜度 官方不存在的珍惜度则直接覆盖字符串
	rarityorder = 2, -- 珍惜度的排序 用于按优先级排序 基本没啥用
	raritycorlor = { 189 / 255, 73 / 255, 73 / 255, 1 }, -- {R,G,B,A}
	skins = { normal_skin = avatar_name, ghost_skin = 'ghost_' .. avatar_name .. '_build' },
	build_name_override = avatar_name,
	share_bigportrait_name = avatar_name .. '_none',
})


return MakePlayerCharacter(avatar_name, prefabs, assets, common_postinit, master_postinit, prefabs)
